//  ========== 
//  = 通用搜索JS = 
//  = K字带头 2015-08-27 = 
//  ========== 

//通用搜索js
var word;
var Comurl = "";
function searchInit(){
	$('.base_search_input').bind('keyup', function(event){
		if (event.keyCode == 38 || event.keyCode == 40){

			var key = event.keyCode - 39;
			var ss = $('.base_search_drop_activity').index() + key;
			$('.base_search_drop>ul>li').removeClass('base_search_drop_activity');
			$('#baseSearchDropLI'+ss).addClass('base_search_drop_activity');
			$('.base_search_input').val($('.base_search_drop_activity').text());

			$('.base_search_type').val($('.base_search_drop_activity').data('type'));
		}else if(event.keyCode == 13){
			if($('.base_search_drop').attr('opacity') != 0 && $('.base_search_drop_activity').text() != '' ){
				//searchHistory();

				searchHistory($('.base_search_drop_activity').text(), $('.base_search_drop_activity').data('type'));
				//$('.base_search_input').val($('.base_search_drop_activity').text());
				//('.base_search_btn').submit();
			}
		}else{
			word = $('.base_search_input').val();
			search();
		}
	});

	$('.base_search_input').bind('blur', function(){
		$('.base_search_drop').css('opacity','0');
	});
	$('.base_search_input').bind('focus', function(){
		$('.base_search_drop').css('opacity','1');
	})

	$('.base_search_tab').on('click', 'li', function(){
		$('.base_search_tab>ul>li').removeClass('selected');
		$(this).addClass('selected');
		$('.base_search_form>form').attr('action', $(this).data('url'));
	});

	$('.base_search_drop').on('click', 'li', function(){

		searchHistory($(this).text(), $(this).data('type'));
	})

	searchAssgin();
}

function jumpSearch(resault, type){
	$('.base_search_input').val(resault);
	$('.base_search_type').val(type);
	$('.base_search_form>form').submit();
}

function getUserId(){
	var userId = $('#userId').val();
	if(userId == null || userId == ""){
		userId = $('#userSessionId').val();;
	}
	return userId;
}
function search(){
	//keyup propertychange
	//jQuery.support.cors = true; $this->user
	var url = Comurl+"/tip/list";/*/tip/list可修改为对应路径*/
	var userId = getUserId();
	var word = $('.base_search_input').val();
	$('.base_search_drop>ul').html('');
	

	$.ajax({
		type:'post',
		datatype:'JSONP',
		jsonp: 'jsoncallback',
		url:url,
		data:{'userId': userId, 'word':word},
		success:function(res){
			if(res.data != null){
				var dataP = res.data.tips;
				
				$('.base_search_drop>ul').html('');
				$.each(dataP, function(n, value){
					$('.base_search_drop>ul').append('<li id="baseSearchDropLI'+n+'" data-type='+value['type']+'>'+
						value['word']+'</li>')
					
				})
			}


		},
		error:function(res){
			
		}
	})

}

function searchHistory(resault, type){
	var url = Comurl+"/tip/saveClick";/*/tip/saveClick可修改为对应路径*/
	var userId = getUserId();


	$.ajax({
		type:'post',
		datatype:'JSONP',
		jsonp: 'jsoncallback',
		url:url,
		data:{'userId': userId, 'word':word, 'result': resault, 'type':type},
		success:function(res){
			

			jumpSearch(resault, type);//成功之后再进行跳转
		},
		error:function(res){
			
		}
	})
}

function searchAssgin(){
	$('.base_search_input').val($('#searchKeyword').val());
}
